package com.example.function.project.model.pojo;


import com.example.common.support.ApplicationContextProvider;
import com.example.function.project.model.one.ProjectRole;
import com.example.function.project.model.one.ProjectUserRole;
import com.example.function.project.service.IProjectRoleService;
import com.example.function.project.service.IProjectUserRoleService;
import com.example.function.sys.model.one.SysUser;
import com.example.function.sys.service.ISysUserService;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Description: <ProjectUserRolePojo vo>. <br>
 * <p>
 * generate time:2021-7-27 18:02:19
 *
 * @author hanqf
 * @version V1.0
 */
@Data
public class ProjectUserRolePojo implements Serializable {
    private static final long serialVersionUID = 1L;

    /*
     * 主键id
     */
    private Long id;


    /*
     * 项目id
     */
    private Long projectId;


    /*
     * 用户id
     */
    private Long userId;

    private SysUser sysUser;


    /*
     * 项目角色id
     */
    private Long projectRoleId;
    private ProjectRole projectRole;


    /**
     * 实体对象复制到数据对象
     */
    public static ProjectUserRolePojo wrapper(ProjectUserRole projectUserRole) {
        ProjectUserRolePojo projectUserRolePojo = new ProjectUserRolePojo();
        BeanUtils.copyProperties(projectUserRole, projectUserRolePojo);
        final IProjectUserRoleService projectUserRoleService = (IProjectUserRoleService) ApplicationContextProvider.getBean("projectUserRoleService");
        //封装用户
        final ISysUserService sysUserService = (ISysUserService) ApplicationContextProvider.getBean("sysUserService");
        final SysUser sysUser = sysUserService.findById(projectUserRolePojo.getUserId());
        projectUserRolePojo.setSysUser(sysUser);

        //封装项目角色
        final IProjectRoleService projectRoleService = (IProjectRoleService) ApplicationContextProvider.getBean("projectRoleService");
        final ProjectRole projectRole = projectRoleService.findById(projectUserRolePojo.getProjectRoleId());
        projectUserRolePojo.setProjectRole(projectRole);

        return projectUserRolePojo;
    }

    public static List<ProjectUserRolePojo> wrapper(List<ProjectUserRole> projectUserRoles) {
        List<ProjectUserRolePojo> list = new ArrayList<>();
        for (ProjectUserRole projectUserRole : projectUserRoles) {
            list.add(ProjectUserRolePojo.wrapper(projectUserRole));
        }
        return list;
    }

}



